package com.lcxw.hxzbapi.service;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.lcxw.hxzbapi.dao.mapper.UnionMapper;
import com.lcxw.hxzbapi.entity.pojo.Union;
import com.lcxw.hxzbcommon.common.StringUtil;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Component;

@Component
public class UnionService {

    @Resource
    UnionMapper unionMapper;

    public int insert(Union union) {
        return unionMapper.insert(union);
    }

    public int update(Union union) {
        return unionMapper.updateById(union);
    }

    public List<Union> getAll() {
        return unionMapper.selectList(null);
    }

    public List<Union> getAll(String unionName) {
        if (StringUtil.isEmpty(unionName)) {
            unionName = null;
        }
        QueryWrapper<Union> wrapper = new QueryWrapper<>();
        wrapper.like(unionName != null, "unionName", unionName);
        return unionMapper.selectList(wrapper);
    }

    public JSONObject getAll(String telephone, int pageNum, int pageSize) {
        if (StringUtil.isEmpty(telephone)) {
            telephone = null;
        }
        IPage<Union> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Union> wrapper = new QueryWrapper<>();
        wrapper.eq(telephone != null, "telephone", telephone);
        IPage<Union> unionIPage = unionMapper.selectPage(page, wrapper);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("total", unionIPage.getTotal());
        jsonObject.put("unionList", unionIPage.getRecords());
        return jsonObject;

    }

    public Union selectById(int id) {
        return unionMapper.selectById(id);
    }

    public Union selectByName(String unionName) {
        QueryWrapper<Union> wrapper = new QueryWrapper<>();
        wrapper.eq("unionName", unionName);
        return unionMapper.selectOne(wrapper);
    }

    public List<Union> selectByStatus(String status) {
        QueryWrapper<Union> wrapper = new QueryWrapper<>();
        wrapper.eq(status != null, "status", status);
        return unionMapper.selectList(wrapper);
    }

    public Union selectByTelephone(String telephone) {
        QueryWrapper<Union> wrapper = new QueryWrapper<>();
        wrapper.eq(telephone != null, "telephone", telephone);
        return unionMapper.selectOne(wrapper);
    }


}
